package cn.dapeng.common;

import java.util.HashSet;
import java.util.Set;

/**
 * @BelongProject study
 * @BelongPackage cn.dapeng.common
 * @Description TODO
 * @ClassName RandomNumber
 * @Author xupenghong
 * @Date 2024/8/23 17:08
 * @Version 1.0.0
 */
public class RandomNumber {

    public static final String SEED = "0123456789";

    public static String getRandomNumber(int length) {
        char[] chars = SEED.toCharArray();
        int size = chars.length;
        char temp;
        for (int i = 0; i < size; i++) {
            int index = (int) (Math.random() * size);
            temp = chars[index];
            chars[index] = chars[i];
            chars[i] = temp;
        }

        return new String(chars, 0, length);
    }

    public static void main(String[] args) {
        int testTimes = 10000;
        int length = 6;

        for(int i = 0; i < testTimes; i ++) {
            String result = getRandomNumber(length);
            test(result, length);
        }
    }

    public static void test(String result, int length) {
        System.out.println(result);
        Set<Character> set = new HashSet<>();
        char[] chars = result.toCharArray();

        for (char aChar : chars) {
            set.add(aChar);
        }
        if(set.size() != length) {
            System.out.println("Oops");
            return;
        }
    }
}
